package com.ziyun.erp.web.modules.store.storeGoodsCategory.controller;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ziyun.erp.modules.store.storeGoodsCategory.entity.StoreGoodsCategoryTwdEntity;
import com.ziyun.erp.modules.store.storeGoodsCategory.service.StoreGoodsCategoryTwdService;
import io.renren.common.utils.UserUtil;
import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;


/**
 * 图文宝商品分类表
 *
 * @author 'lu'
 * @email
 * @date 2018-12-22 16:24:11
 */
@RestController
@RequestMapping("storeGoodsCategory/storegoodscategorytwd")
public class StoreGoodsCategoryTwdController {
    @Autowired
    private StoreGoodsCategoryTwdService storeGoodsCategoryTwdService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("storeGoodsCategory:storegoodscategorytwd:list")
    public R list(@RequestBody Map<String, Object> params) {
        PageUtils page = storeGoodsCategoryTwdService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{categoryId}")
    @RequiresPermissions("storeGoodsCategory:storegoodscategorytwd:info")
    public R info(@PathVariable("categoryId") Integer categoryId) {
        StoreGoodsCategoryTwdEntity storeGoodsCategoryTwd = storeGoodsCategoryTwdService.selectById(categoryId);


        return R.ok().put("storeGoodsCategoryTwd", storeGoodsCategoryTwd);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("storeGoodsCategory:storegoodscategorytwd:save")
    public R save(@RequestBody StoreGoodsCategoryTwdEntity storeGoodsCategoryTwd) {
        String categoryName = storeGoodsCategoryTwd.getCategoryName();
        String categoryType = storeGoodsCategoryTwd.getCategoryType();
        storeGoodsCategoryTwd.setCreateName(UserUtil.getCurrentUser().getUsername());
        int count = storeGoodsCategoryTwdService.selectCount(new EntityWrapper<>(new StoreGoodsCategoryTwdEntity())
                .eq("category_name", categoryName)
                .eq("category_type", categoryType)
                .eq("del", 0));
        if (count > 0) {
            return R.error("相同门店类型下 分类名称已存在");
        }
        storeGoodsCategoryTwdService.insert(storeGoodsCategoryTwd);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("storeGoodsCategory:storegoodscategorytwd:update")
    public R update(@RequestBody StoreGoodsCategoryTwdEntity storeGoodsCategoryTwd) {
        ValidatorUtils.validateEntity(storeGoodsCategoryTwd);
        storeGoodsCategoryTwdService.updateById(storeGoodsCategoryTwd);//全部更新
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("storeGoodsCategory:storegoodscategorytwd:delete")
    public R delete(@RequestParam("id") Integer id) {
        storeGoodsCategoryTwdService.deleteUpdate(id);
        return R.ok();
    }

    @RequestMapping("/deleteAll")
    @RequiresPermissions("storeGoodsCategory:storegoodscategorytwd:delete")
    public R deleteAll(@RequestParam("ids") String ids) {
        storeGoodsCategoryTwdService.deleteAllUpdate(ids);
        return R.ok();
    }
}
